package com.sumainfor.pc.market.service;

import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.CompuUtils;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.dao.HouCaiWuMapper;
import com.sumainfor.pc.market.feign.Zfeign.ZpcGmioFeign;
import com.sumainfor.pc.market.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.market.feign.ZuulFeign.ZuulFeign;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Meng
 * @ClassName HouCaiWuService
 * @date 2020/3/17 15:51
 * @description: TODO
 */
@Service
public class HouCaiWuService {

  @Autowired
    HouCaiWuMapper houCaiWuMapper;

  @Autowired
  ZpcGmioFeign zpcGmioFeign;

  @Autowired
  ZzuulFeign zuulFeign;

    /**
     * 根據條件查詢房源
     * @param params
     * @return
     */
  public List<Map<String,Object>> getHoulist(Map<String,Object> params){
      return houCaiWuMapper.getHoulist(params);
  }




  /**
   *公共事业费统计
   * @param params
   * @param pager
   * @return
   */
  public JsonResult getGEWlist(Map<String,Object> params, Pager pager){


    JsonResult result = new JsonResult();

      pager.setPageno((Integer) params.get("pageno"));
      pager.setPagesize((Integer) params.get("pagesize"));
    pager.setPagerNecessary(params,pager);
    PageUtils pageUtils = new PageUtils();

    //increorstock(1增量 2存量)
    Integer cout =  houCaiWuMapper.getGWEHouCount(params);
    if(cout == 0){
      return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
    }else {
      List<Map<String, Object>> getGWEHoulist = houCaiWuMapper.getGWEHoulist(params);
      for (Map<String,Object> map1:getGWEHoulist){


   String deptId = MapUtils.getString(map1,"MiId");
          Map<String,Object> fuwuMap =  zuulFeign.getDeptInfoById(deptId);
          map1.putAll(fuwuMap);

        double Wat = 0;
        double WatOut = 0;
        double Ele = 0;
        double EleOut = 0;
        double Gas = 0;
        double GasOut = 0;
        double Net = 0;
        double NetOut = 0;

        double ShouRuMoney = 0;
        double ZhiChuMoney = 0;
        double ChaMoney = 0;//差额
        double MaoSumMoney = 0; //毛利率
        double MaoMoney = 0;//每间冒利率



        map1.put("BiType",2);//收入
        map1.put("BiOutType",4);//支出
        map1.put("PubType",1);//水
        Map<String,Object> mapWat = zpcGmioFeign.getGEW(map1);
        Map<String,Object> mapWatOut = zpcGmioFeign.getGEWOut(map1);
        System.out.println("11111111111111");
        System.out.println(mapWat);
        System.out.println(mapWatOut);

        Wat = CompuUtils.add(Wat,MapUtils.getDouble(mapWat,"SumMoney"));
        WatOut = CompuUtils.add(WatOut,MapUtils.getDouble(mapWatOut,"SumMoney"));


        map1.put("PubType",2);//电
        Map<String,Object> mapEle = zpcGmioFeign.getGEW(map1);
        Map<String,Object> mapEleOut = zpcGmioFeign.getGEWOut(map1);

        Ele = CompuUtils.add(Ele,MapUtils.getDouble(mapEle,"SumMoney"));
        EleOut = CompuUtils.add(EleOut,MapUtils.getDouble(mapEleOut,"SumMoney"));






        map1.put("PubType",3);//燃
        Map<String,Object> mapGas = zpcGmioFeign.getGEW(map1);
        Map<String,Object> mapGasOut = zpcGmioFeign.getGEWOut(map1);

        Gas = CompuUtils.add(Gas,MapUtils.getDouble(mapGas,"SumMoney"));
        GasOut = CompuUtils.add(GasOut,MapUtils.getDouble(mapGasOut,"SumMoney"));







        map1.put("PubType",4);//宽
        Map<String,Object> mapNet = zpcGmioFeign.getGEW(map1);
        Map<String,Object> mapNetOut = zpcGmioFeign.getGEWOut(map1);

        Net = CompuUtils.add(Net,MapUtils.getDouble(mapNet,"SumMoney"));
        NetOut = CompuUtils.add(NetOut,MapUtils.getDouble(mapNetOut,"SumMoney"));





        map1.put("BiType",6);//账单类型为退租账单
        map1.put("BiOutType",3);//支出


        List<Map<String,Object>> ceshiOne = zpcGmioFeign.getGEWTuiZu(map1);
        if (ceshiOne!=null&&ceshiOne.size()>0){

          for (Map<String,Object> mapoo:ceshiOne){

            if (MapUtils.getDouble(mapoo,"NetReturnMoney")>0){
              Net = CompuUtils.add(Net,MapUtils.getDouble(mapoo,"NetReturnMoney"));
            }else{
              NetOut = CompuUtils.add(NetOut,  Math.abs(MapUtils.getDouble(mapoo,"NetReturnMoney")));
            }


            if (MapUtils.getDouble(mapoo,"GasReturnMoney")>0){
              Gas = CompuUtils.add(Gas,MapUtils.getDouble(mapoo,"GasReturnMoney"));
            }else{
              GasOut = CompuUtils.add(GasOut,Math.abs(MapUtils.getDouble(mapoo,"GasReturnMoney")));
            }




            if (MapUtils.getDouble(mapoo,"EleReturnMoney")>0){
              Ele = CompuUtils.add(Ele,MapUtils.getDouble(mapoo,"EleReturnMoney"));

            }else{
              EleOut = CompuUtils.add(EleOut,Math.abs(MapUtils.getDouble(mapoo,"EleReturnMoney")));
            }


            if (MapUtils.getDouble(mapoo,"WatReturnMoney")>0){
              Wat = CompuUtils.add(Wat,MapUtils.getDouble(mapoo,"WatReturnMoney"));
            }else{
              WatOut = CompuUtils.add(WatOut,Math.abs(MapUtils.getDouble(mapoo,"WatReturnMoney")));
            }


          }

        }
        List<Map<String,Object>> ceshiTwo = zpcGmioFeign.getGEWTuiZuOut(map1);

        if (ceshiTwo !=null&&ceshiTwo.size()>0){

          for (Map<String,Object> maptt:ceshiTwo){


            if (MapUtils.getDouble(maptt,"NetReturnMoney")>0){
              Net = CompuUtils.add(Net,MapUtils.getDouble(maptt,"NetReturnMoney"));
            }else{
              NetOut = CompuUtils.add(NetOut,Math.abs(MapUtils.getDouble(maptt,"NetReturnMoney")));
            }


            if (MapUtils.getDouble(maptt,"GasReturnMoney")>0){
              Gas = CompuUtils.add(Gas,MapUtils.getDouble(maptt,"GasReturnMoney"));
            }else{
              GasOut = CompuUtils.add(GasOut,Math.abs(MapUtils.getDouble(maptt,"GasReturnMoney")));
            }




            if (MapUtils.getDouble(maptt,"EleReturnMoney")>0){
              Ele = CompuUtils.add(Ele,MapUtils.getDouble(maptt,"EleReturnMoney"));

            }else{
              EleOut = CompuUtils.add(EleOut,Math.abs(MapUtils.getDouble(maptt,"EleReturnMoney")));
            }


            if (MapUtils.getDouble(maptt,"WatReturnMoney")>0){
              Wat = CompuUtils.add(Wat,MapUtils.getDouble(maptt,"WatReturnMoney"));
            }else{
              WatOut = CompuUtils.add(WatOut,Math.abs(MapUtils.getDouble(maptt,"WatReturnMoney")));
            }
          }
        }


        ShouRuMoney = CompuUtils.add(CompuUtils.add(Wat,Ele),CompuUtils.add(Gas,Net));
        ZhiChuMoney = CompuUtils.add(CompuUtils.add(WatOut,EleOut),CompuUtils.add(GasOut,NetOut));
        ChaMoney = CompuUtils.reduce(ShouRuMoney,ZhiChuMoney);
        if (ChaMoney!=0){
          MaoSumMoney = CompuUtils.div(ChaMoney,ShouRuMoney,2);
          MaoMoney = CompuUtils.div(ChaMoney,MapUtils.getDouble(map1,"ProLoculus"),2);
        }



        map1.put("Wat",Wat);
        map1.put("WatOut",WatOut);
        map1.put("Ele",Ele);
        map1.put("EleOut",EleOut);
        map1.put("Gas",Gas);
        map1.put("GasOut",GasOut);
        map1.put("Net",Net);
        map1.put("NetOut",NetOut);
        map1.put("ChaMoney",ChaMoney);
        map1.put("MaoSumMoney",MaoSumMoney);
        map1.put("MaoMoney",MaoMoney);
      }
      return pageUtils.getJsonResult(getGWEHoulist,params,cout);
    }

  }

  /**
   * 租金收入毛利表
   * @param params
   * @param pager
   * @return
   */
  public JsonResult getRoomOutlist(Map<String,Object> params, Pager pager){

    JsonResult result = new JsonResult();

      pager.setPageno((Integer) params.get("pageno"));
      pager.setPagesize((Integer) params.get("pagesize"));
    pager.setPagerNecessary(params,pager);
    PageUtils pageUtils = new PageUtils();

    //increorstock(1增量 2存量)
    Integer cout =  houCaiWuMapper.getGWEHouCount(params);
    if(cout == 0){
      return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
    }else {
      List<Map<String, Object>> getGWEHoulist = houCaiWuMapper.getGWEHoulist(params);
      for (Map<String,Object> map1:getGWEHoulist){


          String deptId = MapUtils.getString(map1,"MiId");
          Map<String,Object> fuwuMap =  zuulFeign.getDeptInfoById(deptId);
          map1.putAll(fuwuMap);



          if (MapUtils.getString(params,"StartTime")!=null&&MapUtils.getString(params,"EndTime")!=null){
              map1.put("StartTime",params.get("StartTime"));
              map1.put("EndTime",params.get("EndTime"));
          }




      double room = 0;
      double roomout = 0;

          double ChaMoney = 0;//差额
          double MaoSumMoney = 0; //毛利率
          double MaoMoney = 0;//每间冒利率
      map1.put("BiType",1);
      map1.put("DepositType",1);
        map1.put("BiOutType",1);//统计业主
      List<Map<String,Object>> mapRoomList = zpcGmioFeign.getRoomMoney(map1);
      if (mapRoomList !=null&&mapRoomList.size()>0){
          for (Map<String,Object> mapRoom:mapRoomList){

              if (MapUtils.getInteger(mapRoom,"FirstMon")==0) {
                  room = CompuUtils.add(room, MapUtils.getDouble(mapRoom, "SumMoney"));//代表收入的房租
              }else{
                  Map<String,Object> ceshione= houCaiWuMapper.getHouUnifiedPricing(mapRoom);
                  room = CompuUtils.add(room, CompuUtils.reduce(MapUtils.getDouble(mapRoom, "SumMoney"),MapUtils.getDouble(ceshione, "UnifiedPricing")));
              }

          }

      }


        Map<String,Object> mapRoomOut = zpcGmioFeign.getGEWOut(map1);
        roomout =  CompuUtils.add(roomout,MapUtils.getDouble(mapRoomOut,"SumMoney"));



          map1.put("BiType",6);//统计退租里面的房租
          map1.put("BiOutType",3);//统计退租里面的房租

          List<Map<String,Object>> ListOne = zpcGmioFeign.getGEWTuiZu(map1);
          if (ListOne.size()>0&&ListOne !=null){
              for (Map<String,Object> mapOne:ListOne){

                  if (MapUtils.getDouble(mapOne,"RetreatMoney")>0){
                      room = CompuUtils.add(room,MapUtils.getDouble(mapOne,"RetreatMoney"));
                  }else{
                      room = CompuUtils.reduce(room,Math.abs(MapUtils.getDouble(mapOne,"RetreatMoney")));
                  }
              }
          }

          List<Map<String,Object>> ListTwo = zpcGmioFeign.getGEWTuiZuOut(map1);

          if (ListTwo.size()>0&&ListTwo !=null){
              for (Map<String,Object> mapTwo:ListTwo){

                  if (MapUtils.getDouble(mapTwo,"RetreatMoney")>0){
                      room = CompuUtils.add(room,MapUtils.getDouble(mapTwo,"RetreatMoney"));
                  }else{
                      room = CompuUtils.reduce(room,Math.abs(MapUtils.getDouble(mapTwo,"RetreatMoney")));
                  }
              }
          }


          ChaMoney = CompuUtils.reduce(room,roomout);//差额
          if (ChaMoney !=0){
              MaoSumMoney = CompuUtils.div(ChaMoney,room,2);
              MaoMoney = CompuUtils.div(ChaMoney,MapUtils.getDouble(map1,"ProLoculus"),2);
          }

          map1.put("room",room);
          map1.put("roomout",roomout);
          map1.put("ChaMoney",ChaMoney);
          map1.put("MaoSumMoney",MaoSumMoney);
          map1.put("MaoMoney",MaoMoney);


          //统计免租期内的租金收入
          double roomMian = 0;

          String dateTime = MapUtils.getString(map1,"dateStart");
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
          Date date = new Date();
          try {
               date = simpleDateFormat.parse(dateTime);
          } catch (ParseException e) {
              e.printStackTrace();
          }

          Date endDate = DateUtils.addDateMonths(date,MapUtils.getInteger(map1,"RentHoliday"));

          String endDateTime = simpleDateFormat.format(endDate);


          Map<String,Object> mapmian = new HashMap<>();
          mapmian.put("HouId",map1.get("HouId"));
          mapmian.put("StartTime",dateTime);
          mapmian.put("EndTime",endDateTime);


          mapmian.put("BiType",1);
          mapmian.put("DepositType",1);

          List<Map<String,Object>> mapRoomListss = zpcGmioFeign.getRoomMoney(mapmian);
          if (mapRoomListss !=null&&mapRoomListss.size()>0){
              for (Map<String,Object> mapRoom:mapRoomListss){

                  if (MapUtils.getInteger(mapRoom,"FirstMon")==0) {
                      roomMian = CompuUtils.add(roomMian, MapUtils.getDouble(mapRoom, "SumMoney"));//代表收入的房租
                  }else{
                      Map<String,Object> ceshione= houCaiWuMapper.getHouUnifiedPricing(mapRoom);
                      roomMian = CompuUtils.add(roomMian, CompuUtils.reduce(MapUtils.getDouble(mapRoom, "SumMoney"),MapUtils.getDouble(ceshione, "UnifiedPricing")));
                  }

              }

          }

          mapmian.put("BiType",6);//统计退租里面的房租
          mapmian.put("BiOutType",3);//统计退租里面的房租

          List<Map<String,Object>> ListOnes = zpcGmioFeign.getGEWTuiZu(map1);
          if (ListOnes.size()>0&&ListOnes !=null){
              for (Map<String,Object> mapOne:ListOnes){

                  if (MapUtils.getDouble(mapOne,"RetreatMoney")>0){
                      roomMian = CompuUtils.add(roomMian,MapUtils.getDouble(mapOne,"RetreatMoney"));
                  }else{
                      roomMian = CompuUtils.reduce(roomMian,Math.abs(MapUtils.getDouble(mapOne,"RetreatMoney")));
                  }
              }
          }

          List<Map<String,Object>> ListTwos = zpcGmioFeign.getGEWTuiZuOut(map1);

          if (ListTwos.size()>0&&ListTwos !=null){
              for (Map<String,Object> mapTwo:ListTwos){

                  if (MapUtils.getDouble(mapTwo,"RetreatMoney")>0){
                      roomMian = CompuUtils.add(roomMian,MapUtils.getDouble(mapTwo,"RetreatMoney"));
                  }else{
                      roomMian = CompuUtils.reduce(roomMian,Math.abs(MapUtils.getDouble(mapTwo,"RetreatMoney")));
                  }
              }
          }





          map1.put("roomMian",roomMian);



      }




      return pageUtils.getJsonResult(getGWEHoulist,params,cout);

    }


  }





}
